package offerv1;

import leetcodev1.链表.Solution;

public class Offer15 {
    public int hammingWeight1(int n) {
        int count = 0, tmp = 1;
        while (tmp < n) {
            tmp *= 2;
        }
        tmp /= 2;
        while (n != 0) {
            if (n > tmp) {
                n -= tmp;
                count++;
            }
            tmp /= 2;
        }

        return count;
    }

    //32位比一遍
    public int hammingWeight(int n) {
        int ret = 0;
        for (int i = 0; i < 32; i++) {
            if ((n & (1 << i)) != 0) {
                ret++;
            }
        }
        return ret;
    }
}
